package Model;

import java.sql.SQLException;
import java.util.Calendar;
import java.util.Date;
import java.util.Timer;

import javax.servlet.*;
import javax.servlet.annotation.WebListener;
import javax.servlet.http.*;

import DBAccess.DBAccess;
import Model.Matchmaking.Matchmaker;
import Model.Matchmaking.MatchmakerTask;
import Model.Matchmaking.NewsletterTask;

@WebListener
public class StartupServlet implements ServletContextListener{

	@Override
	public void contextDestroyed(ServletContextEvent arg0) {
		try {
			DBAccess.con.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	@Override
	public void contextInitialized(ServletContextEvent arg0) {
		new DBAccess();
		Timer timer = new Timer();
		Calendar calendar = Calendar.getInstance();
		NewsletterTask ntask = new NewsletterTask();
		MatchmakerTask mtask = new MatchmakerTask();
		
//		calendar.set(Calendar.DAY_OF_WEEK,Calendar.FRIDAY);
//		System.out.println(calendar.toString());
		
		Date startTime = calendar.getTime();
//		//run weekly
		timer.schedule(ntask, startTime, 1000*60*60*24*7);
//		timer.schedule(ntask, startTime, 100);
		//run daily
//		timer.schedule(mtask, startTime, 100*60*60*24);
		timer.schedule(mtask, startTime, 100*60*60*24);

		
		
	}

}